package com.oe.market.modules.ele.model.dao;

import com.google.gson.Gson;
import com.oe.market.modules.common.model.ADaoBase;
import com.oe.market.modules.ele.model.bean.Account;
import com.oe.market.modules.ele.model.bean.Cookie;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

public class AccountDao extends ADaoBase<Account> {
    private static final Logger log = LoggerFactory.getLogger(AccountDao.class);

    public static AccountDao me = new AccountDao();
    @Override
    public Account getThisClassInstance() {
        return new Account();
    }

    @Override
    public String getTableName() {
        return "oeasy_ele_account";
    }

    public String useOnce(Integer accountId) throws SQLException {
        Account account;
        {
            String sql = "select * from `" + getTableName() + "` where " + KeyValueEquation("account_id",(accountId));
            List<Account> accounts = find(sql);
            if (0 == accounts.size())
                return "该账户不存在";
            account = accounts.get(0);
        }

        {
            Integer use = account.getUse() + 1;
            Integer todayUse = account.getTodayUse() + 1;
            Integer lastUseTime = Math.toIntExact(System.currentTimeMillis() / 1000);
            String sql = "UPDATE `" + getTableName() + "` SET `use`=" + use
                    +", `today_use`=" + todayUse + ", `last_use_time`=" + lastUseTime
                    + " WHERE (`account_id`='"+ accountId + "');";

            int ret = executeSql(sql);
            if (0 == ret) {
                return "更新账户信息失败";
            }
        }

        return null;
    }

    public String addMaxUse(Integer accountId, Integer score) {
        Account account;
        {
            String sql = "select * from `" + getTableName() + "` where " + KeyValueEquation("account_id",(accountId));
            List<Account> accounts = null;
            try {
                accounts = find(sql);
            } catch (SQLException e) {
                e.printStackTrace();
            }
            if (null != accounts && 0 == accounts.size())
                return "该账户不存在";
            account = accounts.get(0);
        }


        Integer maxUse = account.getMaxUse() + score;
        Integer lastUseTime = Math.toIntExact(System.currentTimeMillis() / 1000);
        String sql = "UPDATE `" + getTableName() + "` SET `max_use`=" + maxUse
                +" `last_use_time`=" + lastUseTime
                + " WHERE (`account_id`='"+ accountId + "');";

        return update(sql);

    }

    public Account searchAccountId(Integer accountId) {
        String sql = "select * from `" + getTableName() + "` where " + KeyValueEquation("account_id",(accountId));
        try {
            List<Account> accounts = find(sql);
            if (0 != accounts.size())
                return accounts.get(0);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public String updatePhone(Integer accountId, String phone) {
        String sql = "UPDATE `" + getTableName() + "` SET `phone`='" + phone
                +"' WHERE (`account_id`="+ accountId + ");";
        return update(sql);
    }

    public String bindCookieByAccount(Integer accountId, Integer cookieId) {
        String sql = "UPDATE `" + getTableName() + "` SET `cookie_id`=" + cookieId
                +" WHERE (`account_id`='"+ accountId + "');";
        return update(sql);
    }
}
